import { Injectable } from '@nestjs/common'
import { User } from '@prisma/client'
import { PrismaService } from './../common/prisma.service'
import { CreateSoftDto } from './dto/create-soft.dto'
import { UpdateSoftDto } from './dto/update-soft.dto'

@Injectable()
export class SoftService {
	constructor(private prisma: PrismaService) {}
	create(dto: CreateSoftDto, user: User) {
		return this.prisma.soft.create({
			data: {
				...dto,
				User: { connect: { id: user.id } }
			}
		})
	}

	async findAll(page = 1, limit = 10) {
		return {
			data: {
				page,
				rows: limit,
				total: await this.prisma.soft.count(),
				items: await this.prisma.soft.findMany({
					skip: (page - 1) * limit,
					take: limit
				})
			}
		}
	}

	findOne(id: number) {
		return this.prisma.soft.findFirst({ where: { id } })
	}

	update(id: number, dto: UpdateSoftDto) {
		return this.prisma.soft.update({ where: { id }, data: { ...dto } })
	}

	remove(id: number) {
		return this.prisma.soft.deleteMany({ where: { id } })
		// delete - 如果要删除的数据不存在,会报错; deleteMany 则不会
		// return this.prisma.soft.delete({ where: { id } })
	}
}
